package com.ecs.to_plc_service.service.impl;

import com.ecs.to_plc_service.domain.ProductOrder;
import com.ecs.to_plc_service.service.ProductClient;
import com.ecs.to_plc_service.service.ProductOrderService;
import com.ecs.to_plc_service.utils.JsonUtils;
import com.fasterxml.jackson.databind.JsonNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.UUID;

/**
 * Created by dyh on 2019/4/15.
 */
@Service
public class ProductOrderServiceImpl implements ProductOrderService {


    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private ProductClient productClient;

    @Override
    public ProductOrder save(int userId, int productId) {
        //获取商品详情
//        Map<String, Object> productMap = restTemplate.getForObject("http://totos-service/api/v1/product/find?id=" + productId, Map.class);

        String response = productClient.findById(productId);
        JsonNode jsonNode = JsonUtils.str2JsonNode(response);
//        System.out.print(object);
        ProductOrder order = new ProductOrder();
        order.setCreatTime(new Date());
        order.setUserId(userId);
        order.setTradeNo(UUID.randomUUID().toString());
        order.setProductName(jsonNode.get("name").toString());
        order.setPrice(Integer.parseInt(jsonNode.get("price").toString()));
//        order.setProductName(productMap.get("name").toString());
//        order.setPrice(Integer.parseInt(productMap.get("price").toString()));


        return order;
    }
}
